<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="打印机序列号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入打印机序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主体名称" prop="subjectName">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入主体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="MAC地址" prop="macAddress">
        <el-input
          v-model="queryParams.macAddress"
          placeholder="请输入MAC地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="authStatus">
        <el-select v-model="queryParams.authStatus" placeholder="请选择认证状态" clearable>
          <el-option
            v-for="dict in dict.type.printer_auth_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="usageStatus">
        <el-select v-model="queryParams.usageStatus" placeholder="请选择使用状态" clearable>
          <el-option
            v-for="dict in dict.type.printer_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证时间">
        <el-date-picker
          v-model="daterangeAuthTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['printer_auth_records:printer_auth_records:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['printer_auth_records:printer_auth_records:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['printer_auth_records:printer_auth_records:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['printer_auth_records:printer_auth_records:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="printer_auth_recordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="打印机序列号" align="center" prop="serialNumber" />
      <el-table-column label="主体名称" align="center" prop="subjectName" />
      <el-table-column label="MAC地址" align="center" prop="macAddress" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="认证状态" align="center" prop="authStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.printer_auth_status" :value="scope.row.authStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="usageStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.printer_status" :value="scope.row.usageStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="认证时间" align="center" prop="authTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.authTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务点所在省" align="center" prop="province" />
      <el-table-column label="服务点所在市" align="center" prop="city" />
      <el-table-column label="服务点所在区" align="center" prop="district" />
      <el-table-column label="服务点详细地址" align="center" prop="addressDetail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['printer_auth_records:printer_auth_records:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['printer_auth_records:printer_auth_records:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打印机认证记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="打印机序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入打印机序列号" />
        </el-form-item>
        <el-form-item label="主体名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入主体名称" />
        </el-form-item>
        <el-form-item label="MAC地址" prop="macAddress">
          <el-input v-model="form.macAddress" placeholder="请输入MAC地址" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="认证状态" prop="authStatus">
          <el-select v-model="form.authStatus" placeholder="请选择认证状态">
            <el-option
              v-for="dict in dict.type.printer_auth_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="usageStatus">
          <el-select v-model="form.usageStatus" placeholder="请选择使用状态">
            <el-option
              v-for="dict in dict.type.printer_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="认证时间" prop="authTime">
          <el-date-picker clearable
            v-model="form.authTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择认证时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务点所在省" prop="province">
          <el-input v-model="form.province" placeholder="请输入服务点所在省" />
        </el-form-item>
        <el-form-item label="服务点所在市" prop="city">
          <el-input v-model="form.city" placeholder="请输入服务点所在市" />
        </el-form-item>
        <el-form-item label="服务点所在区" prop="district">
          <el-input v-model="form.district" placeholder="请输入服务点所在区" />
        </el-form-item>
        <el-form-item label="服务点详细地址" prop="addressDetail">
          <el-input v-model="form.addressDetail" placeholder="请输入服务点详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrinter_auth_records, getPrinter_auth_records, delPrinter_auth_records, addPrinter_auth_records, updatePrinter_auth_records } from "@/api/printer_auth_records/printer_auth_records";

export default {
  name: "Printer_auth_records",
  dicts: ['printer_status', 'printer_auth_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打印机认证记录表格数据
      printer_auth_recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 服务点详细地址时间范围
      daterangeAuthTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: null,
        subjectName: null,
        macAddress: null,
        phoneNumber: null,
        authStatus: null,
        usageStatus: null,
        authTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serialNumber: [
          { required: true, message: "打印机序列号不能为空", trigger: "blur" }
        ],
        subjectName: [
          { required: true, message: "主体名称不能为空", trigger: "blur" }
        ],
        macAddress: [
          { required: true, message: "MAC地址不能为空", trigger: "blur" }
        ],
        phoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        authStatus: [
          { required: true, message: "认证状态不能为空", trigger: "change" }
        ],
        usageStatus: [
          { required: true, message: "使用状态不能为空", trigger: "change" }
        ],
        authTime: [
          { required: true, message: "认证时间不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "服务点所在省不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "服务点所在市不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "服务点所在区不能为空", trigger: "blur" }
        ],
        addressDetail: [
          { required: true, message: "服务点详细地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询打印机认证记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAuthTime && '' != this.daterangeAuthTime) {
        this.queryParams.params["beginAuthTime"] = this.daterangeAuthTime[0];
        this.queryParams.params["endAuthTime"] = this.daterangeAuthTime[1];
      }
      listPrinter_auth_records(this.queryParams).then(response => {
        this.printer_auth_recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNumber: null,
        subjectName: null,
        macAddress: null,
        phoneNumber: null,
        authStatus: null,
        usageStatus: null,
        authTime: null,
        province: null,
        city: null,
        district: null,
        addressDetail: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAuthTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打印机认证记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPrinter_auth_records(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打印机认证记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePrinter_auth_records(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrinter_auth_records(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打印机认证记录编号为"' + ids + '"的数据项？').then(function() {
        return delPrinter_auth_records(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('printer_auth_records/printer_auth_records/export', {
        ...this.queryParams
      }, `printer_auth_records_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
